package Hot100;

import Tree.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class 二叉树的右视图 {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null) return result;
        Queue<TreeNode> qt = new LinkedList<>();
        qt.offer(root);
        while (!qt.isEmpty()) {
            int len = qt.size();
            for (int i = 0; i < len; i++) {
                TreeNode node = qt.poll();
                if (node.left != null) qt.offer(node.left);
                if (node.right != null) qt.offer(node.right);
                if (i == len - 1) result.add(node.val);
            }
        }
        return result;
    }
}
